//
//  VideoViewController.m
//  EconomyDemo
//
//  Created by qianfeng01 on 15-7-4.
//  Copyright (c) 2015年 xiangmu. All rights reserved.
//

#import "VideoViewController.h"
#import "PictureModel.h"
#import "VideoCell.h"
#import "AFNetworking.h"
#import "VideoModel.h"
#import "PlayViewController.h"
#import "JHRefresh.h"

#define kScreenSize [UIScreen mainScreen].bounds.size
#define kUrl @"http://ipadnews.caijing.com.cn/api/1.0/articles.php?bundleId=cn.com.caijing.android&platform=android4.4.2&appVer=2.4.1&network=wifi&action=articlelist&username=&columnId=325&password=&page=%ld"

@interface VideoViewController () <UITableViewDataSource,UITableViewDelegate>
{
    AFHTTPRequestOperationManager *_manager;
    NSMutableArray *_dataArray;
    NSMutableArray *_dataArr;
    UITableView *_tableView;
}
@property (nonatomic,strong) AFHTTPRequestOperationManager *manager;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSMutableArray *dataArr;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic) NSInteger currentPage;
@property (nonatomic)BOOL isRefreshing;
@property (nonatomic)BOOL isLoadMore;

@end

@implementation VideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    _dataArray=[[NSMutableArray alloc] init];
    _dataArr=[[NSMutableArray alloc] init];
    [self creatTableView];
    [self firstDownload];
    [self creatRefreshView];
}
- (void)creatRefreshView {
    __weak typeof (self) weakSelf = self;
    [self.tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (weakSelf.isRefreshing) {
            return ;
        }
        weakSelf.isRefreshing = YES;
        weakSelf.currentPage = 1;
        
        [weakSelf addTaskWithUrl:[NSString stringWithFormat:kUrl,weakSelf.currentPage] isRefresh:YES];
        
    }];
    [self.tableView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (weakSelf.isLoadMore) {
            return ;
        }
        weakSelf.isLoadMore = YES;
        weakSelf.currentPage ++;
        [weakSelf addTaskWithUrl:[NSString stringWithFormat:kUrl,weakSelf.currentPage] isRefresh:YES];
    }];
    
}
- (void)endRefreshing {
    if (self.isRefreshing) {
        self.isRefreshing = NO;
        [self.tableView headerEndRefreshingWithResult:JHRefreshResultNone];
    }
    if (self.isLoadMore) {
        self.isLoadMore = NO;
        [self.tableView footerEndRefreshing];
    }
}

- (void)firstDownload {
    self.currentPage = 1;
    
    [self addTaskWithUrl:[NSString stringWithFormat:kUrl,self.currentPage] isRefresh:NO];
}

-(void)addTaskWithUrl:(NSString *)url isRefresh:(BOOL)isRefresh{
    __weak typeof(self) weakSelf=self;
    _manager=[[AFHTTPRequestOperationManager alloc] init];
    _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    [weakSelf.manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"成功下载");
        if (responseObject) {
            if (weakSelf.currentPage==1) {
                [weakSelf.dataArray removeAllObjects];
            }
            NSArray *arr=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            for (NSInteger i=0; i<arr.count; i++) {
                NSDictionary *dict=[[NSDictionary alloc] init];
                PictureModel *model=[[PictureModel alloc] init];
                dict=arr[i];
                [model setValuesForKeysWithDictionary:dict];
                model.articleId=dict[@"articleId"];
                model.title=dict[@"title"];
                model.path=dict[@"path"];
                model.thumbnails_url=dict[@"thumbnails_url"][4];
                [weakSelf.dataArray addObject:model];
                NSArray *mediasArr=dict[@"medias"];
                VideoModel *model1=[[VideoModel alloc] init];
                for (NSDictionary *dic in mediasArr) {
                    
                    model1.file=dic[@"file"];
                    [weakSelf.dataArr addObject:model];
                }
                model.url=[NSString stringWithFormat:@"%@%@",model.path,model1.file];
            }
            [self.tableView reloadData];
            [self endRefreshing];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
    }];
    
}
-(void)creatTableView{
    
    _tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-49-64) style:UITableViewStylePlain];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.view addSubview:_tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"VideoCell" bundle:nil] forCellReuseIdentifier:@"VideoCell"];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    VideoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"VideoCell" forIndexPath:indexPath];
    PictureModel *model=self.dataArray[indexPath.row];
    [cell showDataWithModel:model];
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PlayViewController *controller=[[PlayViewController alloc] init];
    VideoModel *model1=self.dataArr[indexPath.row];
    PictureModel *model2=self.dataArray[indexPath.row];
    
    VideoCell *cell=[[VideoCell alloc] init];
    cell.url=[NSString stringWithFormat:@"%@/%@",model2.path,model1.file];
    
    controller.url=[NSString stringWithFormat:@"%@/%@",model2.path,model1.file];

    [self presentViewController:controller animated:YES completion:nil];
    
}





@end
